package cc.linker.steplink.crm.merchanth5.util;

import cc.linker.steplink.crm.merchanth5.pojo.acs.TradeParamPo;
import com.alibaba.fastjson.JSON;

import java.util.*;

public class MapSortUtil {
    // Map的value值降序排序
    public static <K, V extends Comparable<? super V>> Map<K, V> sortDescend(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                int compare = (o1.getValue()).compareTo(o2.getValue());
                return -compare;
            }
        });

        Map<K, V> returnMap = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            returnMap.put(entry.getKey(), entry.getValue());
        }
        return returnMap;
    }

    // Map的value值升序排序
    public static <K, V extends Comparable<? super V>> Map<K, V> sortAscend(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                int compare = (o1.getValue()).compareTo(o2.getValue());
                return compare;
            }
        });

        Map<K, V> returnMap = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            returnMap.put(entry.getKey(), entry.getValue());
        }
        return returnMap;
    }


    public static void main(String[] args) {
        Integer sharePrice = 12;
        Integer originalPrice = 8;
        Integer recomPrice = 16;
        Integer unionBarleyFee =6;
        Integer platformPrice = 4;
        Integer linkerFee = 4;
        Integer income = 50;
        Integer score = 1;
        TradeParamPo po = new TradeParamPo(score,sharePrice,recomPrice,income,unionBarleyFee,platformPrice,linkerFee,originalPrice);
        Map<String,Integer> map = new LinkedHashMap();
        //按照这种顺序put 服务费励<创作奖励<推荐费<支付抽成（联汇）<支付抽成（大麦）<平台抽成<商品成交收益
        map.put("sharePrice",sharePrice);
        map.put("originalPrice",originalPrice);
        map.put("recomPrice",recomPrice);
        map.put("linkerFee",linkerFee);
        map.put("unionBarleyFee",unionBarleyFee);
        map.put("platformPrice",platformPrice);
        map.put("income",income);
        System.out.println(JSON.toJSONString(map));
        //从小到大排序
        map = MapSortUtil.sortAscend(map);
        System.out.println(JSON.toJSONString(map));
        for (Object t:map.keySet()) {
            Integer amount =  map.get(t.toString());
            if(amount>score){
                amount = amount - score;
                score = 0;
            }else {
                score = score - amount;
                amount = 0;
            }
            map.put(t.toString(),amount);
        }
        System.out.println(JSON.toJSONString(map));
        po.setSharePrice(map.get("sharePrice"));
        po.setSharePriceScore(sharePrice-map.get("sharePrice"));
        po.setOriginalPrice(map.get("originalPrice"));
        po.setOriginalPriceScore(originalPrice-map.get("originalPrice"));
        po.setRecomPrice(map.get("recomPrice"));
        po.setRecomPriceScore(recomPrice-map.get("recomPrice"));
        po.setUnionBarleyFee(unionBarleyFee);
        po.setUnionBarleyFeeScore(unionBarleyFee-map.get("unionBarleyFee"));
        po.setPlatformPrice(platformPrice);
        po.setPlatformPriceScore(platformPrice-map.get("platformPrice"));
        po.setLinkerFee(linkerFee);
        po.setLinkerFeeScore(linkerFee-map.get("linkerFee"));
        po.setIncome(map.get("income"));
        po.setIncomeScore(income-map.get("income"));
        System.out.println(JSON.toJSONString(po));
    }
}
